<?php
/* ###############################################################################
 *
 * $Id: xml.php 61 2015-01-15 18:43:13Z expertsoftware.i@gmail.com $
 *
 * システム名
 *   PHP Web フレームワーク [Ponzu]
 *
 * 機能
 *   XML ライブラリ
 *
 * バージョン
 *   0.0.0_0
 *
 * ライセンス
 *   LGPL v2.0
 *
 * 履歴
 *   2007/07/01 K.Sonohara
 *     新規作成。
 *
 * ###############################################################################*/

function attributeNode($n, $name) {
	return mb_convert_encoding($n->getAttribute($name), SITE_ENCODE, "UTF-8");
}

function valueNode($n, $name) {
	$n = _nodesFrom($n, $name);

  if ($n == null) return null;
	return mb_convert_encoding($n->nodeValue, SITE_ENCODE, "UTF-8");
}


function _nodesFrom($e, $name, $a=null, $v=null) {
	$nl = $e->childNodes;
	if($nl) foreach ($nl as $n) {
	if($n->nodeName == $name) {
      if ($a == null) return $n;
			else if ($n->getAttribute($a)  == $v) return $n;
		}
  }
	return null;
}


function nodeFrom($e, $name, $t=null, $l=null) {
	$xp = explode("/", $name);
	$n = $e;
  foreach ($xp as $k => $v) {
  	if($v != "") {
			$a = _nodesFrom($n, $v, $t, $l);
			if($a == null) {
				return null;
			} else {
				$n = $a;
			}
  	}
  }

  return $n;
}


function xsltFrom($xmlname, $xsltname) {
  $xml = xmlFrom($xmlname);
  $xsl = xmlFrom($xsltname);

  return xslt($xml, $xsl);
}


function xmlFrom($xmlname) {
  $x = new DomDocument();
  $x->load($xmlname);
  return $x;
}

function xslt($xml, $xsl, $options=NULL) {
    $processor = new xsltprocessor();

    if ($options != NULL) {
      foreach ($options as $k => $v) {
        $processor->setParameter('', $k, $v);
      }
    }

    $processor->importStyleSheet($xsl);

    return mb_convert_encoding($processor->transformToXML($xml), SITE_ENCODE, "UTF-8");
}
?>
